package com.waypolice.manager.service.impl;

import com.ibm.icu.text.DateFormat;
import com.waypolice.common.pojo.AppraisalItemResult;
import com.waypolice.common.pojo.TaotaoResult;
import com.waypolice.common.utils.DateTimeUtil;
import com.waypolice.common.utils.FastDfsClient;
import com.waypolice.manager.service.ArcAppraisalService;
import com.waypolice.manager.service.ArcCreateService;
import com.waypolice.manager.service.MainMenuService;
import com.waypolice.mapper.*;
import com.waypolice.mapper.handwriting.CmsAppraisalMapperSon;
import com.waypolice.mapper.handwriting.CmsJudItemMapperSon;
import com.waypolice.mapper.handwriting.CmsJudVolumeMapperSon;
import com.waypolice.pojo.*;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 档案著录实现类
 * @author zry
 *
 */
@Service
public class ArcAppraisalServiceImpl implements ArcAppraisalService {
    @Autowired
    private CmsJudVolumeMapper cmsJudVolumeMapper;
    @Autowired
    private CmsAppraisalMapperSon cmsAppraisalMapperSon;
    @Autowired
    private CmsAppraisalMapper cmsAppraisalMapper;
    @Autowired
    private CmsJudItemMapperSon cmsJudItemMapperSon;
    @Autowired
    private CmsJudItemMapper cmsJudItemMapper;
    @Autowired
    private CmsDestroyMapper cmsDestroyMapper;
    @Autowired
    private ArcCreateService arcCreateService;
    @Autowired
    private CmsSinItemMapper cmsSinItemMapper;

    @Override
    public List<CmsJudVolume> selectVolumeByVolId(Long aLong) {
        List<CmsJudVolume> volumes = new ArrayList<>();
        CmsJudVolume cmsJudVolume = cmsJudVolumeMapper.selectByPrimaryKey(aLong);
        volumes.add(cmsJudVolume);
        return volumes;
    }

    @Override
    public List<CmsJudItem> selectItemByItemId(Long aLong) {
        List<CmsJudItem> items = new ArrayList<>();
        CmsJudItem cmsJudItem = cmsJudItemMapper.selectByPrimaryKey(aLong);
        items.add(cmsJudItem);
        return items;
    }

    @Override
    public List<Map<String, Object>> findArchiveTypeByArchiveNum(String itemId) {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        if ("".equals(itemId)) {
            return null;
        } else {
            hashMap.put("itemId", itemId);
        }
        List<Map<String, Object>> arcList = cmsJudItemMapperSon.selecArcByItemId(hashMap);
        return arcList;
    }

    @Override
    public long findItemNumById(String id) {
        List<CmsJudItem> itemLists = arcCreateService.findItemLists(Long.valueOf(id));
        return itemLists.size();
    }

    @Override
    public TaotaoResult inserAppraisalMessage(String apprCode, String apprApartment, String apprPerson, String apprDate, String volId, String itemId, String arcType,
                                              String apprArcview, String apprPersonview, String apprExportsview,
                                              String apprLeaderview, String apprScope, String apprResult, String nextDeadline, String apprOldDate, String apprRemarks) {
        TaotaoResult result = new TaotaoResult();
        CmsAppraisal cmsAppraisal = new CmsAppraisal();
        cmsAppraisal.setApprCode(apprCode);
        cmsAppraisal.setApprApartment(apprApartment);
        cmsAppraisal.setApprPerson(apprPerson);
        cmsAppraisal.setApprDate(apprDate);
        cmsAppraisal.setAppExtrafive(apprOldDate);
        cmsAppraisal.setAppExtrasix(apprRemarks);
        cmsAppraisal.setApprDate(apprDate);
        if ("".equals(volId)) {
            cmsAppraisal.setApprArcId(Long.valueOf(itemId));
            if ("1".equals(arcType)) {
                cmsAppraisal.setApprType(1);
                CmsJudItem item = cmsJudItemMapper.selectByPrimaryKey(Long.valueOf(itemId));
                cmsAppraisal.setAppExtraone(item.getItemTitle());
            } else {
                cmsAppraisal.setApprType(2);
                CmsSinItem item = cmsSinItemMapper.selectByPrimaryKey(Long.valueOf(itemId));
                cmsAppraisal.setAppExtraone(item.getItemTitle());
            }
        } else {
            cmsAppraisal.setApprArcId(Long.valueOf(volId));
            cmsAppraisal.setApprType(0);
            CmsJudVolume volume = cmsJudVolumeMapper.selectByPrimaryKey(Long.valueOf(volId));
            cmsAppraisal.setAppExtraone(volume.getVolTitle());
        }
        cmsAppraisal.setApprArcview(apprArcview);
        cmsAppraisal.setApprPersonview(apprPersonview);
        cmsAppraisal.setApprExportsview(apprExportsview);
        cmsAppraisal.setApprLeaderview(apprLeaderview);
        cmsAppraisal.setApprCreatetime(new Date());
        cmsAppraisal.setApprStatus(0);
        cmsAppraisal.setAppExtratwo(apprScope);
        cmsAppraisal.setAppExtrathree(nextDeadline);
        cmsAppraisal.setAppExtrafour(apprResult);
        int i = cmsAppraisalMapperSon.insertAppraisal(cmsAppraisal);
        if (i != 1) {
            result.setStatus(101);
        } else {
            Integer apprId = cmsAppraisal.getApprId();
            //将卷件状态改为鉴定中
            if ("".equals(volId)) {
                //将单件状态改为鉴定中
                if ("1".equals(arcType)) {
                    CmsJudItem cmsJudItem = cmsJudItemMapper.selectByPrimaryKey(Long.valueOf(itemId));
                    cmsJudItem.setItemStatus(1L);
                    cmsJudItemMapper.updateByPrimaryKeySelective(cmsJudItem);
                } else {
                    CmsSinItem item = cmsSinItemMapper.selectByPrimaryKey(Long.valueOf(itemId));
                    item.setItemStatus(1L);
                    cmsSinItemMapper.updateByPrimaryKeySelective(item);
                }

            } else {
                //将卷及卷内件状态改为鉴定中
                CmsJudVolume cmsJudVolume = cmsJudVolumeMapper.selectByPrimaryKey(Long.valueOf(volId));
                cmsJudVolume.setVolStatus(1);
                cmsJudVolumeMapper.updateByPrimaryKeySelective(cmsJudVolume);
                CmsJudItemExample cmsJudItemExample = new CmsJudItemExample();
                CmsJudItemExample.Criteria criteria = cmsJudItemExample.createCriteria();
                criteria.andItemVolumeIdEqualTo(Long.valueOf(volId));
                List<CmsJudItem> items = cmsJudItemMapper.selectByExample(cmsJudItemExample);
                for (CmsJudItem cmsJudItem : items) {
                    cmsJudItem.setItemStatus(1L);
                    cmsJudItemMapper.updateByPrimaryKeySelective(cmsJudItem);
                }
            }
            result.setMsg(apprId.toString());
            result.setStatus(200);
        }
        return result;
    }

    @Override
    public TaotaoResult updateAppraisalMessage(String apprId, String apprCode, String apprApartment, String apprPerson, String apprDate, String volId,
                                               String itemId, String arcType, String apprArcview, String apprPersonview, String apprExportsview,
                                               String apprLeaderview, String apprScope, String apprResult, String nextDeadline, String apprOldDate, String apprRemarks) {
        TaotaoResult result = new TaotaoResult();
        CmsAppraisalExample example = new CmsAppraisalExample();
        CmsAppraisalExample.Criteria criteria = example.createCriteria();
        criteria.andApprIdEqualTo(Integer.parseInt(apprId));
        List<CmsAppraisal> cmsAppraisals = cmsAppraisalMapper.selectByExample(example);
        CmsAppraisal cmsAppraisal = cmsAppraisals.get(0);
        cmsAppraisal.setApprCode(apprCode);
        cmsAppraisal.setApprApartment(apprApartment);
        cmsAppraisal.setApprPerson(apprPerson);
        cmsAppraisal.setApprDate(apprDate);
        cmsAppraisal.setAppExtrafive(apprOldDate);
        cmsAppraisal.setAppExtrasix(apprRemarks);
        if ("".equals(volId)) {
            cmsAppraisal.setApprArcId(Long.valueOf(itemId));
            if ("1".equals(arcType)) {
                cmsAppraisal.setApprType(1);
            } else {
                cmsAppraisal.setApprType(2);
            }
        } else {
            cmsAppraisal.setApprArcId(Long.valueOf(volId));
            cmsAppraisal.setApprType(0);
        }
        cmsAppraisal.setApprArcview(apprArcview);
        cmsAppraisal.setApprPersonview(apprPersonview);
        cmsAppraisal.setApprExportsview(apprExportsview);
        cmsAppraisal.setApprLeaderview(apprLeaderview);
        cmsAppraisal.setApprCreatetime(DateTimeUtil.getNow());
        cmsAppraisal.setAppExtratwo(apprScope);
        cmsAppraisal.setAppExtrathree(nextDeadline);
        cmsAppraisal.setAppExtrafour(apprResult);
        int i = cmsAppraisalMapper.updateByPrimaryKeySelective(cmsAppraisal);
        if (i != 1) {
            result.setStatus(101);
        } else {
            result.setMsg(apprId.toString());
            result.setStatus(200);
        }
        return result;
    }

    @Override
    public List<CmsAppraisal> selectAllAppraisal() {
        CmsAppraisalExample cmsAppraisalExample = new CmsAppraisalExample();
        CmsAppraisalExample.Criteria criteria = cmsAppraisalExample.createCriteria();
        criteria.getAllCriteria();
        cmsAppraisalExample.setOrderByClause("\"APPR_CREATETIME\" DESC");
        List<CmsAppraisal> cmsAppraisals = cmsAppraisalMapper.selectByExample(cmsAppraisalExample);
        return cmsAppraisals;
    }

    @Override
    public CmsAppraisal loadApprMessageByApprId(String apprId) {
        CmsAppraisal cmsAppraisal = cmsAppraisalMapper.selectByPrimaryKey(Integer.parseInt(apprId));
        return cmsAppraisal;
    }

    @Override
    public TaotaoResult updateAppraisalResultMessage(String apprId, String apprCode, String apprApartment, String apprPerson, String apprDate,
                                                     String apprArcview, String apprPersonview, String apprExportsview, String apprLeaderview, String apprScope, String apprResult, String nextDeadline, String apprOldDate, String apprRemarks) {
        TaotaoResult result = new TaotaoResult();
        CmsAppraisal cmsAppraisal = cmsAppraisalMapper.selectByPrimaryKey(Integer.parseInt(apprId));
        cmsAppraisal.setApprCode(apprCode);
        cmsAppraisal.setApprApartment(apprApartment);
        cmsAppraisal.setApprPerson(apprPerson);
        cmsAppraisal.setApprDate(apprDate);
        cmsAppraisal.setAppExtrafive(apprOldDate);
        cmsAppraisal.setAppExtrasix(apprRemarks);
        cmsAppraisal.setApprArcview(apprArcview);
        cmsAppraisal.setApprPersonview(apprPersonview);
        cmsAppraisal.setApprExportsview(apprExportsview);
        cmsAppraisal.setApprLeaderview(apprLeaderview);
        cmsAppraisal.setApprCreatetime(DateTimeUtil.getNow());
        cmsAppraisal.setApprStatus(1);
        cmsAppraisal.setAppExtratwo(apprScope);
        cmsAppraisal.setAppExtrathree(nextDeadline);
        cmsAppraisal.setAppExtrafour(apprResult);
        int i = cmsAppraisalMapper.updateByPrimaryKeySelective(cmsAppraisal);
        if (i != 1) {
            result.setStatus(101);
        } else {
            result.setMsg(apprId.toString());
            result.setStatus(200);
        }
        return result;
    }

    @Override
    public List<CmsSinItem> selectSinItemByItemId(Long aLong) {
        List<CmsSinItem> cmsSinItems = new ArrayList<>();
        CmsSinItem cmsSinItem = cmsSinItemMapper.selectByPrimaryKey(aLong);
        cmsSinItems.add(cmsSinItem);
        return cmsSinItems;
    }

    @Override
    public List<AppraisalItemResult> selectAllNeedAppraisalArc() {
        List<AppraisalItemResult> finallist = new ArrayList<>();
        //第一步：查询卷内件保存期限到期《暂不查询》
        //第二步：单件保存期限到期《暂不查询》
        //第三步：查询卷内件保密期限到期
        //1:查询所有归档的卷内件
        //2:比较件发文日期+保密时间是否大于当天，如果大于则查出来，放入finallist
        CmsJudItemExample cmsJudItemExample = new CmsJudItemExample();
        CmsJudItemExample.Criteria criteria = cmsJudItemExample.createCriteria();
        List<Long> lists = new ArrayList<>();
        lists.add(0L);//正常
        lists.add(2L);//借阅中
        criteria.andItemStatusIn(lists);//件状态
        criteria.andItemStr6IsNotNull();//保密期限
        criteria.andItemSecretIsNotNull();//密级
        List<CmsJudItem> rows = cmsJudItemMapper.selectByExample(cmsJudItemExample);
        for (CmsJudItem cmsJudItem : rows) {
            AppraisalItemResult appraisalItemResult = new AppraisalItemResult();
            Long itemId = cmsJudItem.getItemId();
            CmsJudItem cmsJudItem1 = cmsJudItemMapper.selectByPrimaryKey(itemId);
            if(cmsJudItem1 != null){
                CmsJudVolume cmsJudVolume = cmsJudVolumeMapper.selectByPrimaryKey(cmsJudItem1.getItmeVolId());
                if(cmsJudVolume != null){
                    Integer volArchiveStatus = cmsJudVolume.getVolArchiveStatus();
                    if (volArchiveStatus == 2) {
                        //取出保密期限
                        String itemStr6 = cmsJudItem.getItemStr6();
                        //取出发文日期
                        String itemCreateTime = cmsJudItem.getItemCreateTime();
                        String year = itemCreateTime.substring(0, 4);
                        String month = itemCreateTime.substring(4, 6);
                        String date = itemCreateTime.substring(6, 8);
                        Integer newYear = Integer.valueOf(year)+Integer.valueOf(itemStr6);
                        String newDate = newYear +"-"+ month +"-"+ date;
                        //string转date，date转毫秒
                        Date datetime = null;
                        datetime = java.sql.Date.valueOf(newDate);
                        long timeOld = datetime.getTime();
                        //当前时间毫秒值
                        long time = System.currentTimeMillis();
                        if (timeOld < time  || timeOld == time ) {
                            appraisalItemResult.setItemId(cmsJudItem.getItemId());
                            //文号
                            appraisalItemResult.setItemName(cmsJudItem.getItemName());
                            //单元号
                            appraisalItemResult.setItemTemporary(cmsJudItem.getItemTemporary());
                            //题名
                            appraisalItemResult.setItemTitle(cmsJudItem.getItemTitle());
                            //发文日期
                            appraisalItemResult.setItemCreateTime(cmsJudItem.getItemCreateTime());
                            //保管期限
                            appraisalItemResult.setItemNum(cmsJudVolume.getVolStorageTime());
                            //责任人
                            appraisalItemResult.setItemRespons(cmsJudItem.getItemRespons());
                            //密级
                            appraisalItemResult.setItemSecret(cmsJudItem.getItemSecret());
                            //保密期限
                            appraisalItemResult.setItemExp4(cmsJudItem.getItemStr6());
                            //归档日期
                            appraisalItemResult.setItemStr1Date(cmsJudItem.getItemStr11Date());
                            //档号
                            appraisalItemResult.setItemStr2(cmsJudItem.getItemStr1());
                            //档案类型1:卷内件2：件
                            appraisalItemResult.setItemExp5("1");
                            //到期类型1：保管期限到期2：保密期限到期
                            appraisalItemResult.setItemExp6("2");
                            //档案分类
                            appraisalItemResult.setItemExp7(cmsJudVolume.getVolClass());
                            //归档单位
                            appraisalItemResult.setItemExp8(cmsJudVolume.getVolArchiveUnit());
                            //件数
                            appraisalItemResult.setItemNumber("1");
                            finallist.add(appraisalItemResult);
                        }
                    }
                }
            }
        }
        //第四步：单件保密期限到期
        //1:查询所有归档的单件
        //2:比较件发文日期+保密时间是否大于当天，如果大于则查出来，放入finallist
        CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
        CmsSinItemExample.Criteria sincriteria = cmsSinItemExample.createCriteria();
        sincriteria.andItemStatusIn(lists);
        sincriteria.andItemArcStatusEqualTo(1L);
        sincriteria.andItemSecretIsNotNull();
        sincriteria.andItemExp4IsNotNull();
        List<CmsSinItem> cmsSinItems = cmsSinItemMapper.selectByExample(cmsSinItemExample);
        if(!cmsSinItems.isEmpty()){
            for (CmsSinItem cmsSinItem : cmsSinItems) {
                AppraisalItemResult appraisalItemResult = new AppraisalItemResult();
                //取出保密期限
                String itemStr6 = cmsSinItem.getItemExp4();
                //取出发文日期
                String itemCreateTime = cmsSinItem.getItemCreateTime();
                String year = itemCreateTime.substring(0, 4);
                String month = itemCreateTime.substring(4, 6);
                String date = itemCreateTime.substring(6, 8);
                Integer newYear = Integer.valueOf(year)+Integer.valueOf(itemStr6);
                String newDate = newYear +"-"+ month +"-"+ date;
                //string转date，date转毫秒
                Date datetime = null;
                datetime = java.sql.Date.valueOf(newDate);
                long timeOld = datetime.getTime();
                //当前时间毫秒值
                long time = System.currentTimeMillis();
                if (timeOld < time  || timeOld == time ) {
                    appraisalItemResult.setItemId(cmsSinItem.getItemId());
                    //文号
                    appraisalItemResult.setItemName(cmsSinItem.getItemName());
                    //单元号
                    appraisalItemResult.setItemTemporary(cmsSinItem.getItemTemporary());
                    //题名
                    appraisalItemResult.setItemTitle(cmsSinItem.getItemTitle());
                    //发文日期
                    appraisalItemResult.setItemCreateTime(cmsSinItem.getItemCreateTime());
                    //保管期限
                    appraisalItemResult.setItemNum(cmsSinItem.getItemNum());
                    //责任人
                    appraisalItemResult.setItemRespons(cmsSinItem.getItemRespons());
                    //密级
                    appraisalItemResult.setItemSecret(cmsSinItem.getItemSecret());
                    //保密期限
                    appraisalItemResult.setItemExp4(cmsSinItem.getItemExp4());
                    //归档日期
                    appraisalItemResult.setItemStr1Date(cmsSinItem.getItemStr1Date());
                    //档号
                    appraisalItemResult.setItemStr2(cmsSinItem.getItemStr1());
                    //档案类型1:卷内件2：件
                    appraisalItemResult.setItemExp5("2");
                    //到期类型1：保管期限到期2：保密期限到期
                    appraisalItemResult.setItemExp6("2");
                    //档案分类
                    appraisalItemResult.setItemExp7(cmsSinItem.getItemExp1());
                    //归档单位
                    appraisalItemResult.setItemExp8(cmsSinItem.getItemExp2());
                    //件数
                    appraisalItemResult.setItemNumber("1");
                    finallist.add(appraisalItemResult);
                }
            }
        }
        return finallist;
    }
}
